Skip to content

Fix get_history_metadata() raising YFPricesMissingError for valid tickers (#2745) - #2795

Open
Jaypatel1511 wants to merge 2 commits into
ranaroussi:devfrom
Jaypatel1511:fix/get-history-metadata-missing-error
Open

Jaypatel1511 wants to merge 2 commits into
ranaroussi:devfrom
Jaypatel1511:fix/get-history-metadata-missing-error

Conversation

@Jaypatel1511

Copy link
Copy Markdown
Contributor

Summary

Fixes #2745 — get_history_metadata() raised YFPricesMissingError for valid, non-delisted tickers (e.g. ROG.SW) even after history() succeeded, because the optional intraday enrichment call failed and was not caught.

Two bugs fixed in scrapers/history.py:

Bug 1 — good metadata overwritten by failed fetch (line ~240)

history() unconditionally wrote self._history_metadata = meta after every fetch. When the intraday enrichment request returned no data, meta was {}, silently destroying the currency, exchangeName, timezone, etc. already cached from a prior successful history() call.

Fix: only overwrite _history_metadata when the new meta is non-empty, or when it has never been set:

# Before
self._history_metadata = meta

# After
if meta or self._history_metadata is None:
    self._history_metadata = meta

Bug 2 — YFPricesMissingError propagates from optional enrichment call

get_history_metadata() called _get_history_cache(period="5d", interval="1h") to populate tradingPeriods. For some tickers/exchanges (e.g. Swiss exchange), intraday data is unavailable and this raised YFPricesMissingError, even though all core metadata was already cached.

Fix: catch YFPricesMissingError around the enrichment call and return whatever metadata is already available:

# Before
self._get_history_cache(period="5d", interval="1h")['prices']

# After
try:
    self._get_history_cache(period="5d", interval="1h")['prices']
except YFPricesMissingError:
    pass  # tradingPeriods is optional enrichment; return whatever metadata we have

Test plan

  • Added tests/test_get_history_metadata.py with 4 regression cases
  • Verified good metadata is preserved after a failed intraday fetch
  • Verified get_history_metadata() returns cached metadata instead of raising
  • All 23 unit tests pass

@etbala

etbala commented May 1, 2026

Copy link
Copy Markdown
Contributor

get_history_metadata is used in yf.Ticker, so the tests for it should go in test_ticker.py.

Also this if statement does do what you said, but the way it is written it reads like you are checking if either meta or history_metadata is None, something a little more explicit might be nice.

if meta or self._history_metadata is None:
    self._history_metadata = meta

@ValueRaider

Copy link
Copy Markdown
Collaborator

but the way it is written it reads like ...

@etbala TBF codebase is already littered with similar statements, mostly in history.py

@ValueRaider

Copy link
Copy Markdown
Collaborator

…kers (ranaroussi#2745)

Two bugs in scrapers/history.py:

1. history() always overwrote _history_metadata with {} on a failed fetch,
   destroying previously cached good metadata from a prior successful call.
   Fix: only update _history_metadata when the new meta is non-empty or
   when it has never been set.

2. get_history_metadata() let YFPricesMissingError from the optional
   intraday tradingPeriods enrichment call propagate to the caller, even
   when currency, exchangeName, timezone, etc. were already cached.
   Fix: catch YFPricesMissingError around the enrichment call and return
   whatever metadata is already available.
`call` and `patch` were imported but never referenced in the test file,
causing the Ruff F401 check to fail in CI.
@Jaypatel1511
Jaypatel1511 force-pushed the fix/get-history-metadata-missing-error branch from acbd782 to e500447 Compare May 5, 2026 21:59
@Jaypatel1511

Copy link
Copy Markdown
Contributor Author

Rebased onto dev.

@ValueRaider
ValueRaider changed the base branch from main to dev May 6, 2026 10:42
@etbala

etbala commented May 8, 2026

Copy link
Copy Markdown
Contributor

get_history_metadata is used in yf.Ticker, so the tests for it should go in test_ticker.py.

@Jaypatel1511 Just in case it wasn't clear, this is still an issue.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_history_metadata() raises YFPricesMissingError for valid tickers when intraday data is unavailable

3 participants